//数据库操作快捷方法
const util = require("./util.js");

/**删除商品订单 */
function deleteShopOrder(info, callback=null){
  const app = getApp();
  const userInfo = app.globalData.userInfo;
  if(userInfo){
    var hts = userInfo.orderShops;
    for(var i=0; i<hts.length; i++){
      if(hts[i].key == info.key){
        hts.splice(i, 1);
        break;
      }
    }
    const curdata = {
      type:"setOrderShops",
      type2:"set",
      openid:app.globalData.openid,
      list:hts
    }
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data:curdata,
      complete: res => {
        console.log('setOrderShops: ', res);
        util.getAndSetUserInfo(callback);//
      }
    })
  }
}
/**删除门票订单 */
function deleteTicketOrder(info, callback=null){
  const app = getApp();
  const userInfo = app.globalData.userInfo;
  if(userInfo){
    var hts = userInfo.orderTickets;
    for(var i=0; i<hts.length; i++){
      if(hts[i].key == info.key){
        hts.splice(i, 1);
        break;
      }
    }
    const curdata = {
      type:"setOrderTickets",
      type2:"set",
      openid:app.globalData.openid,
      list:hts
    }
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data:curdata,
      complete: res => {
        console.log('setOrderTickets: ', res);
        util.getAndSetUserInfo(callback);//
      }
    })
  }
}
/**删除酒店订单 */
function deleteHotelOrder(info, callback=null){
  const app = getApp();
  const userInfo = app.globalData.userInfo;
  if(userInfo){
    var hts = userInfo.orderHotels;
    for(var i=0; i<hts.length; i++){
      if(hts[i].key == info.key){
        hts.splice(i, 1);
        break;
      }
    }
    const curdata = {
      type:"setOrderHotels",
      type2:"set",
      openid:app.globalData.openid,
      list:hts
    }
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      data:curdata,
      complete: res => {
        console.log('setOrderHotels: ', res);
        util.getAndSetUserInfo(callback);//
      }
    })
  }
}


module.exports = {
  deleteShopOrder,
  deleteTicketOrder,
  deleteHotelOrder
}